<?php

namespace App\Http\Controllers\Servers;

use App\Http\Controllers\Controller;
use App\Models\ServerModel;
use Illuminate\Http\Request;
use Yaroslam\SSH2\Session\Connection\UserPasswordConnection;

class ServerController extends Controller
{
    public function page(Request $request, string $connection, string $ip)
    {
        return view('server.index', ['title' => $ip, 'ip' => $ip, 'connection' => $connection]);
    }

    public function session(Request $request, string $connection, string $ip)
    {
        return view('server.sedncommand', ['title' => $ip, 'ip' => $ip, 'connection' => $connection,
            'links' => [
                ['href' => '/servers/list', 'text' => 'Сервера'], ['text' => 'Управление сессией',
                    'href' => route('server.page', ['ip' => $ip, 'connection' => $connection])],
                ['text' => 'Сценарии', 'href' => route('server.scenario', ['ip' => $ip, 'connection' => $connection])]]]); //todo доделать
    }

    public function scenarios(Request $request, string $connection, string $ip)
    {
        return view('server.scenario', ['title' => $ip, 'ip' => $ip, 'connection' => $connection,
            'links' => [
                ['href' => '/servers/list', 'text' => 'Сервера'], ['text' => 'Управление сессией',
                    'href' => route('server.page', ['ip' => $ip, 'connection' => $connection])],
                ['text' => 'Сценарии', 'href' => route('server.scenario', ['ip' => $ip, 'connection' => $connection])]]]); //todo доделать
    } //todo доделать

    public function admin(Request $request, string $ip)
    {
        return view('server.admin.admin-panel', ['ip' => $ip, 'title' => 'Администрирование', 'links' => [
            ['href' => '/servers/list', 'text' => 'Сервера'], ['text' => 'Управление сессиями',
                'href' => route('server.admin.sessions', ['ip' => $ip]),
            ]]]);
    }

    public function sessionsList(Request $request, string $ip)
    {
        $server = ServerModel::query()->where('ip_address', '=', $ip)->first();

        return view('server.admin.sessions-list', ['title' => 'Список сессий', 'server' => $server,
            'links' => [
                ['href' => '/servers/list', 'text' => 'Сервера'], ['text' => 'Администрирование',
                    'href' => route('server.admin', ['ip' => $ip]),
                ]]]);
    }

    public function sessionView(Request $request, $ip, $id)
    {
        return view('server.admin.session-view', ['sessionId' => $id, 'title' => 'Просмотр сессии']);
    }

    public function service(Request $request, $connection, $ip, $service)
    {
        return redirect()->route('session.'.$service.'.index', ['connection' => $connection,
            'ip' => $ip, 'service' => $service]);
    }

    //    public function scenario(Request  $request,  $connection,  $ip,  $service) {
    //        return red
    //        $s = new \Yaroslam\SSH2\Session\ChainSession(new UserPasswordConnection(), ['port' => 22, 'host' => '194.87.110.114',
    //            'properties' => ['user' => 'root', 'password' => '3w3uxhXmmh']]);
    //        $s->initChain();
    //        $s = generateSession($request->scenario, $s);
    //        $s->apply();
    //    }
    //
    //    private function generateSession($scenario, \Yaroslam\SSH2\Session\ChainSession $session) {
    //        foreach ($scenario as $s) {
    //            if ($s['type'] == 'exec') {
    //                $session->exec($s['command']);
    //            } elseif ($s['type'] == 'if') {
    //                $session->if($s['command'], $s['condition']);
    //                $session->then();
    //                $session = generateSession($s['then'], $session);
    //                $session->endThen();
    //
    //                $session->else();
    //                $session = generateSession($s['else'], $session);
    //                $session->endElse();
    //
    //                $session->endIf();
    //            } elseif ($s['type'] == 'for') {
    //                $session->for($s['start'], $s['stop'], $s['step']);
    //                $session = generateSession($s['body'], $session);
    //                $session->endFor();
    //            }
    //
    //        }
    //        return $session;
    //    }
}
